package com.frank.code_notes.C01_array;

import cn.hutool.core.lang.Console;

/**
 * @Description 844.比较含退格的字符串
 * @Author zhoujr
 * @Data 2024/11/26 15:08
 */
public class L844CompareStringBackspaces {

    /**
     * @Description 使用双指针法，重点在于退格的移动
     */
    public static boolean backspaceCompare(String s, String t) {
        int pointerOne = s.length() - 1;
        int pointerTwo = t.length() - 1;
        int oneNum = 0, twoNum = 0;
        while (pointerOne >= 0 || pointerTwo >= 0) {
            while (pointerOne >= 0) {
                if (s.charAt(pointerOne) == '#') { // 累计退格数量
                    pointerOne--;
                    oneNum++;
                } else if (oneNum > 0) { // 一个一个退
                    pointerOne--;
                    oneNum--;
                } else {
                    break;
                }
            }

            while (pointerTwo >= 0) {
                if (t.charAt(pointerTwo) == '#') {
                    pointerTwo--;
                    twoNum++;
                } else if (twoNum > 0) {
                    pointerTwo--;
                    twoNum--;
                } else {
                    break;
                }
            }
            if (pointerOne >= 0 && pointerTwo >= 0) { // 都在区间内
                if (s.charAt(pointerOne) != t.charAt(pointerTwo)) {
                    return Boolean.FALSE;
                }
            }else if (pointerOne >= 0 || pointerTwo >= 0) { // 一个在区间内，一个不在区间内
                return Boolean.FALSE;
            }
            pointerOne--;
            pointerTwo--;
        }

        return Boolean.TRUE;
    }

    public static void main(String[] args) {
        Console.log(backspaceCompare("xywrrmp", "xywrrmu#p"));
    }

}
